﻿Imports System.Data.SqlClient

Public Class CnData

    Private WithEvents cn As SqlConnection

    Public Sub New()
        cn = New SqlConnection("Data Source=" & My.Settings.BDD_path & ";Initial Catalog=" & My.Settings.BDD & ";User ID=" & My.Settings.BDD_login & ";Password=" & My.Settings.BDD_pass)
    End Sub

    Public Function Open() As Boolean
        Try
            If cn.State = ConnectionState.Closed Then cn.Open()
            Return True
        Catch ex As Exception
            MsgBox("Erreur de fermeture de la base de données")
            Return False
        End Try
    End Function

    Public Function Close() As Boolean
        Try
            If cn.State = ConnectionState.Open Then cn.Close()
            Return True
        Catch ex As Exception
            MsgBox("Erreur de fermeture de la base de données")
            Return False
        End Try
    End Function

    Public Function getCn() As SqlConnection
        Return cn
    End Function

    Public Function connected() As Boolean
        If cn.State = ConnectionState.Closed Then Return False
        Return True
    End Function

    Shared Sub con_StateChange(ByVal Sender As Object, _
                 ByVal E As StateChangeEventArgs) Handles cn.StateChange
        Console.WriteLine("{0} - {1}", "ConnectionState", E.CurrentState.ToString())
    End Sub

End Class
